	<?php
	defined('BASEPATH') OR exit('No direct script access allowed');

	class Catalog extends CI_Controller {

		// protected $dictArray = array();
		public function __construct()
    {
      parent::__construct();

      $this->config->load();
      $this->load->helper('url_helper');
      $this->load->database();
      $this->load->model('catalog_model');
    }

    //for ads
    public function add(){
      if (!isset($_SESSION['username']))
      {
          redirect('user/login');
      }


      $this->load->helper('form');
      $this->load->library('form_validation');
      $this->form_validation->set_rules( 'title', '标题', 'required' );
            
      $data['title'] = '添加分类';

      
      if ($this->form_validation->run() === FALSE)
      {
          $data['cataloglist'] = $this->catalog_model->get(0);
     
          $this->load->view('templates/header', $data);
          $this->load->view('catalogadd');
          $this->load->view('templates/footer');
          
      }
      else
      {
          $this->catalog_model->add();
          redirect('catalog/index');
      }

    }

   //for ads
    public function status($id,$enable){
     if (!isset($_SESSION['username']))
        {
            redirect('user/login');
        }

        if ($id > 0) {
            $this->catalog_model->setEnable($id, $enable);
        }
   
        redirect('catalog/index');

    }
    
    public function update($id){
       if (!isset($_SESSION['username']))
        {
            redirect('user/login');
        }
        
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules( 'title', '标题', 'required' );
       
         $data['catalogInfo'] = $this->catalog_model->getOne($id);
        if ($this->form_validation->run() === FALSE)
        {
            $data['cataloglist'] = $this->catalog_model->get(0);
     
            $this->load->view('templates/header', $data);
            $this->load->view('catalogupdate');
            $this->load->view('templates/footer');
            
        }
        else
        {
            //echo "this.".$userId;
            $this->catalog_model->update($id);

            redirect('catalog/index');
        }
    }


    public function delete($id)
    {
        if (!isset($_SESSION['username'])) {  
            redirect('user/login');  ;  
        }  
        
        // $data['title'] = '删除用户';
        
        if ($id > 0) {
            $this->catalog_model->delete($id);
        }
   
        redirect('catalog/index');

    }

    public function index(){
      if (!isset($_SESSION['username'])) {  
          redirect('user/login');  ;  
       }  
  
      date_default_timezone_set("PRC");
      $data['list'] = $this->catalog_model->getAll();
          
      $this->load->view('templates/header', $data);
      $this->load->view('catalog', $data);
      $this->load->view('templates/footer');
    }
    

    
  }
